﻿using B.Auth.APi.Application.Command.Users;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand
{
    public class UpdataUserHand : IRequestHandler<UpdataUser, ApiResult<int>>
    {
        private readonly IBaseRepository<User> db;

        public UpdataUserHand(IBaseRepository<User> db)
        {
            this.db = db;
        }

        public Task<ApiResult<int>> Handle(UpdataUser request, CancellationToken cancellationToken)
        {
           var result=new ApiResult<int>();
            var user=new User();
            user.Id = request.Id;
            user.Name = request.Name;
            user.Sex = request.Sex;
            user.FromProvince = request.FromProvince;
            user.FromCity = request.FromCity;
            user.FromCounty = request.FromCounty;
            user.Addressmessage = request.Addressmessage;
            user.Del = request.Del;
            user.CreateUserName = "张三";

            user.CreateTime=request.CreateTime;
            user.UpdateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); 
            user.UodateUserName = "张三";
            user.PassWord = request.PassWord;
            user.UserName = request.UserName;
            user.departid = request.departid; 
            var useshow = db.GetAll().Where(x => x.Name == request.Name&&x.Id!=request.Id&&x.Del==0).Count();
            if (useshow==1)
            {

                result.Message = "用户名不能重复";
                result.Code = ApiEnum.fail;
                return Task.FromResult(result);

            }

            db.Update(user);
            result.Message="成功";
            return Task.FromResult(result); 
        }
    }
}
